/*
 * Created by IntelliJ IDEA.
 * User: seandoyle
 * Date: 7/29/12
 * Time: 8:47 PM
 */
package com.spotalog.server.service.search;

import javax.inject.Singleton;

import com.google.appengine.api.search.Consistency;
import com.google.appengine.api.search.Cursor;
import com.google.appengine.api.search.Document;
import com.google.appengine.api.search.Field;
import com.google.appengine.api.search.Index;
import com.google.appengine.api.search.IndexSpec;
import com.google.appengine.api.search.ListRequest;
import com.google.appengine.api.search.Query;
import com.google.appengine.api.search.QueryOptions;
import com.google.appengine.api.search.SearchService;
import com.google.appengine.api.search.SearchServiceFactory;
import com.google.appengine.api.search.SortExpression;
import com.google.appengine.api.search.SortOptions;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
import com.spotalog.shared.annotations.Radius;
import com.spotalog.shared.model.Spot;

public class SearchModule extends AbstractModule {

	protected void configure() {
		bindConstant().annotatedWith(Radius.class).to(Double.MAX_VALUE);
		bind(new TypeLiteral<Function<Spot, Document>>() {}).to(SpotToDocument.class);
		bind(Splitter.class).toInstance(Splitter.on(':').omitEmptyStrings());
		bind(Joiner.class).toInstance(Joiner.on(':').skipNulls());
	}

	@Provides
	@Singleton
	SearchService provideSearchService() {
		return SearchServiceFactory.getSearchService();
	}

	@Provides
	@Singleton
	Index provideSearchIndex(SearchService searchService) {
		return searchService.getIndex(IndexSpec.newBuilder().setName("spotalog")
				.setConsistency(Consistency.PER_DOCUMENT).build());
	}

	@Provides
	SortExpression.Builder provideSortExpressionBuilder() {
		return SortExpression.newBuilder();
	}

	@Provides
	QueryOptions.Builder provideQueryOptionsBuilder() {
		return QueryOptions.newBuilder();
	}

	@Provides
	Query.Builder provideQueryBuilder() {
		return Query.newBuilder();
	}

	@Provides
	Cursor.Builder provideCursor() {
		return Cursor.newBuilder();
	}

	@Provides
	SortOptions.Builder provideSortOptionsBuilder() {
		return SortOptions.newBuilder();
	}

	@Provides
	ListRequest.Builder provideListRequest() {
		return ListRequest.newBuilder();
	}

	@Provides
	Document.Builder provideDocumentBuilder() {
		// TODO set Locale
		return Document.newBuilder();
	}

	@Provides
	Field.Builder provideFieldBuilder() {
		// TODO set Locale
		return Field.newBuilder();
	}
}
